home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3170 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: using MALLOC() w/ 2-d arrays
  5. Date: Fri, 26 Jan 1996 17:34:16 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <3108F478.1831@cmt.lpr.mail.carel.fi>
  8. References: <4e9nm2$nna@cville-srv.wam.umd.edu>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. jeffrey d squires wrote:
  16. > I have a function that takes as one of its arguments:
  17. > unsigned char image[MAX_Y][MAX_X}
  18. > I have a number of images which is determined at run time,
  19. > therefore I can't declare the size of image_array[][MAX_Y][MAX_X].
  20. > In other words, I want to be able to allocate space for as many
  21. > of these images as I need at run time.  I've tried everything
  22. > (except the correct way).
  23. > I think I need something of the form:
  24. > unsigned char * image_array[MAX_Y][MAX_X];
  25. > image_array = (unsigned char *) malloc(.....sizeof(unsigned char));
  26. > I know that if I declare:
  27. > unsigned char image_array[10][MAX_Y][MAX_X];
  28. > I can successfully send this to the function, but what
  29. > if I don't know that I'll need ten?  Can anyone tell
  30. > me the proper solution?  Thank you.
  31.  
  32. You could try:
  33.  
  34.     typedef unsigned char    ia_type[MAX_Y][MAX_X];
  35.  
  36.     ia_type    *image_array;
  37.  
  38.     image_array = (ia_type*)calloc(numelems, sizeof(ia_type));
  39.  
  40.     image_array[0][1][2] = 'X';
  41.     ...
  42.  
  43. Later,
  44. AriL
  45. -- 
  46. All my opinions are mine and mine alone.
  47.